﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class StaffController
    {
        #region value
        private readonly StaffBO _staffBo=new StaffBO();
        #endregion

        #region ham lay du lieu

        public IQueryable<RelaxStaffDataContract> GetStaffDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _staffBo.GetStaffDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(RelaxStaffDataContract staffDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var staff = new RelaxManagerMember
                {
                    name = staffDataContract.Name,
                    gender = staffDataContract.Gender,
                    birthday = staffDataContract.Birthday,
                    identification = staffDataContract.Identification,
                    tel = staffDataContract.Tel,
                    degree = staffDataContract.Degree,
                    ministry = staffDataContract.Ministry,
                    salaryratio = staffDataContract.SalaryRatio,
                    salary = staffDataContract.Salary,
                    allowance = staffDataContract.Allowance,
                    hometown = staffDataContract.Hometown,
                    datebegin = staffDataContract.DateBegin,
                    dateend = staffDataContract.DateEnd,
                    image = staffDataContract.Image,
                    email = staffDataContract.Email,
                    active = staffDataContract.Active
                };
                _staffBo.Add(staff);
                var first = _staffBo.GetStaffDataContract().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                    iD = Convert.ToInt32(first.Id);
                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update

        public Boolean Update(RelaxStaffDataContract staffDataContract)
        {
            using (var scope = new TransactionScope())
            {
               
                var staff = new RelaxManagerMember
                {
                    id = Convert.ToInt32(staffDataContract.Id),
                    name = staffDataContract.Name,
                    gender = staffDataContract.Gender,
                    birthday = staffDataContract.Birthday,
                    identification = staffDataContract.Identification,
                    tel = staffDataContract.Tel,
                    degree = staffDataContract.Degree,
                    ministry = staffDataContract.Ministry,
                    salaryratio = staffDataContract.SalaryRatio,
                    salary = staffDataContract.Salary,
                    allowance = staffDataContract.Allowance,
                    hometown = staffDataContract.Hometown,
                    datebegin = staffDataContract.DateBegin,
                    dateend = staffDataContract.DateEnd,
                    image = staffDataContract.Image,
                    email = staffDataContract.Email,
                    active = staffDataContract.Active
                };
                _staffBo.Update(staff);
               
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _staffBo.Delete(id);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}
